<template>
  <div>
    <el-card>
      <el-page-header icon="" content="添加用户" title="用户管理">
      </el-page-header>
      <el-form
        ref="userFormRef"
        :model="userForm"
        :rules="userFormRules"
        label-width="80px"
        class="demo-ruleForm"
      >
        <el-row :gutter="10">
          <el-col :span="8">
            <el-form-item label="用户名" prop="userName">
              <el-input v-model="userForm.userName" /> </el-form-item
          ></el-col>
          <el-col :span="8">
            <el-form-item label="密码" prop="password">
              <el-input
                type="password"
                v-model="userForm.password"
              /> </el-form-item
          ></el-col>
          <el-col :span="8">
            <el-form-item label="性别" prop="gender">
              <el-select
                v-model="userForm.gender"
                style="width: 100%"
                placeholder="请选择"
              >
                <el-option
                  v-for="item in options"
                  :label="item.label"
                  :value="item.value"
                  :key="item.value"
                />
              </el-select> </el-form-item
          ></el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="8">
            <el-form-item label="角色" prop="role">
              <el-select
                v-model="userForm.role"
                style="width: 100%"
                placeholder="请选择"
              >
                <el-option
                  v-for="item in roleOptions"
                  :label="item.label"
                  :value="item.value"
                  :key="item.value"
                />
              </el-select> </el-form-item
          ></el-col>
          <el-col :span="16">
            <el-form-item label="个人简介" prop="introduction">
              <el-input
                v-model="userForm.introduction"
                type="textarea"
              /> </el-form-item
          ></el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="用户头像" prop="avatar">
              <Upload
                :avatar="userForm.avatar"
                @fileChange="handleChange"
              ></Upload> </el-form-item
          ></el-col>
        </el-row>
        <el-form-item class="btn-right">
          <el-button type="primary" @click="submitForm()"> 提交 </el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import Upload from '@/components/upload/Upload.vue'
import upload from '@/util/upload'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'

const router = useRouter()
const userFormRef = ref()
const userForm = reactive({
  userName: '',
  password: '',
  role: null, //1管理员，2编辑
  gender: null,
  introduction: '',
  avatar: '',
  file: null
})
const userFormRules = reactive({
  userName: [
    {
      required: true,
      // pattern: /0\d{2,3}-\d{7,8}/,
      message: '请输入用户名',
      trigger: 'blur'
    }
  ],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
  role: [{ required: true, message: '请选择角色', trigger: 'blur' }],
  gender: [{ required: true, message: '请选择性别', trigger: 'blur' }],
  introduction: [{ required: true, message: '请输入简介', trigger: 'blur' }],
  avatar: [{ required: true, message: '请上传头像', trigger: 'blur' }]
})

const options = [
  { label: '保密', value: 0 },
  { label: '男', value: 1 },
  { label: '女', value: 2 }
]
const roleOptions = [
  { label: '管理员', value: 1 },
  { label: '编辑', value: 2 }
]

const handleChange = (file) => {
  userForm.avatar = URL.createObjectURL(file)
  // console.log(userForm.avatar)
  userForm.file = file
}

const submitForm = () => {
  userFormRef.value.validate(async (valid) => {
    if (valid) {
      console.log('userForm', userForm)
      const res = await upload('/adminApi/user/addUser', userForm)
      // console.log(res.data);
      if (res.data.code == '0000') {
        ElMessage.success(res.data.msg)
        router.push(`/user-manage/UserList`)
      }
    }
  })
}
</script>

<style scoped lang="scss">
.demo-ruleForm {
  margin-top: 50px;
}
.btn-right {
  float: right;
}
</style>
